﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;

namespace WPGame.Graphics
{
    //7ung
    class Sprite
    {
        private Texture2D m_texture;
        private Vector2 m_position;
        private Rectangle m_Frame;
        private Rectangle m_BaseRect;
        private int m_curFrame;
        private int m_colFrame;
        private float m_rotation; // duoc tinh bang` radiance
        private Vector2 m_origin; // origin la dai luong quy dinh goc de~ xoay 
        private Vector2 m_scale;
        private Color m_color;
        private float m_depth;

        private Rectangle m_Bound;

        //http://stackoverflow.com/questions/2804395/c-sharp-4-0-can-i-use-a-color-as-an-optional-parameter-with-a-default-value
        public Sprite(int col, Rectangle baserect, Vector2? position = null)
        {
            CurFrame = 0;
            ColFrame = col;
            Scale = Vector2.One;
            Origin = Vector2.Zero;
            Color = Color.White;
            BaseRect = baserect;
            m_Bound.Width = m_BaseRect.Width;
            m_Bound.Height = m_BaseRect.Height;
            Position = position.HasValue ? position.Value : new Vector2(0.0f, 0.0f);
            Rotation = 0.0f;
            Depth = 1.0f;
        }

        public Sprite(ContentManager Content, string filename, int col = 1, Rectangle? baserect = null)
        {
            LoadContent(Content, filename);
            CurFrame = 0;
            ColFrame = col;
            Scale = Vector2.One;
            Origin = Vector2.Zero;
            Color = Color.White;
            BaseRect = (baserect.HasValue) ? baserect.Value : new Rectangle(0, 0, Texture.Width, Texture.Height);
            m_Bound.Width = m_BaseRect.Width;
            m_Bound.Height = m_BaseRect.Height;
            Position = new Vector2(0, 0);
            Rotation = 0.0f;
            Depth = 1.0f;
        }


        public Sprite(Sprite sprite)
        {
            this.Position = sprite.Position;
            this.CurFrame = sprite.CurFrame;
            this.ColFrame = sprite.ColFrame;
            this.Rotation = sprite.Rotation;
            this.Origin = sprite.Origin;
            this.Scale = sprite.Scale;
            this.Color = sprite.Color;
            this.Depth = sprite.Depth;
        }

        public void LoadContent(ContentManager Content, string filename)

        {
          if (this.Texture == null)
                try
                {
                    this.Texture = Content.Load<Texture2D>(filename);
                }
                catch
                {
                    //throw new Exception("content not found");
                }
        }

        /// <summary>
        /// tăng ô Frame lên một đơn vị
        /// </summary>
        public void setFrame()
        {
            CurFrame++;
            if (CurFrame > ColFrame)
                CurFrame = 0;
        }

  
        /// <summary>
        /// tăng biến Rotation lên đơn vị radian
        /// </summary>
        /// <param name="_gametime"></param>
        public void Rotate()
        {
            Rotation += (float) Math.PI * 0.034f;
            if (Rotation >= (float) Math.PI * 2)
                Rotation = 0;
        }

        virtual public void Render(SpriteBatch spritebatch)
        {
            spritebatch.Draw(
                this.Texture, 
                this.Position,
                this.BaseRect,
                this.Color,
                this.Rotation,
                this.Origin,
                this.Scale,
                SpriteEffects.None,
                this.Depth);
        }

        #region Property

        public Texture2D Texture
        {
            get { return m_texture; }
            set { m_texture = value; }
        }

        public Rectangle Frame
        {
            get { return m_Frame; }
            set
            {
                m_Frame = value;
                m_Bound.Width = m_BaseRect.Width;
                m_Bound.Height = m_BaseRect.Height;
            }
        }

        public Rectangle BaseRect
        {
            get { return m_BaseRect; }
            set
            {
                m_BaseRect = value;
            }
        }

        public Vector2 Position
        {
            get { return m_position; }
            set
            {
                m_position = value;
                m_Bound.Location = new Point((int)Position.X, (int)Position.Y);
            }
        }

        public int CurFrame
        {
            get { return m_curFrame; }
            set
            {
                if (value >= 0)
                    m_curFrame = value;
                else
                    m_curFrame = 0;
            }
        }

        public int ColFrame
        {
            get { return m_colFrame; }
            set
            {
                if (value >= 1)
                    m_colFrame = value;
                else
                    m_colFrame = 1;
            }
        }

        public float Rotation
        {
            get { return m_rotation; }
            set
            {
                m_rotation = value % (float)Math.PI;
            }
        }

        public Vector2 Origin
        {
            get { return m_origin; }
            set
            {
                m_origin = value;
            }
        }

        public Vector2 Scale
        {
            get { return m_scale; }
            set
            {
                m_scale = value;
            }
        }

        public Color Color
        {
            get { return m_color; }
            set { m_color = value; }
        }

        public float Depth
        {
            get { return m_depth; }
            set
            {
                m_depth = value;
            }
        }

        public Rectangle Bound
        {
            get { return m_Bound; }
            set { m_Bound = value; }
        }

        #endregion


    }
}